package com.service.edu.controller;


import com.service.edu.pojo.Comment;
import com.service.edu.service.CommentService;
import com.service.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-07
 */
@RestController
@RequestMapping("comment")
@Api(description = "课程评论管理")
public class CommentController {

    @Autowired
    private CommentService commentService;


    @PostMapping("page")
    @ApiOperation("分页查询所有的课程评论信息")
    public ResultVo page(
            @ApiParam(name = "pageNum",value = "当前页数")  @RequestParam(defaultValue = "1",value = "pageNum")  Integer pageNum,
            @ApiParam(name = "rows",value = "每页显示条数")  @RequestParam(defaultValue = "10",value = "rows") Integer rows,
            @ApiParam(name = "CommentQuery",value = "课程评论查询条件",required = false) @RequestBody(required = false) Comment comment
    ){
        return commentService.pages(pageNum,rows,comment);
    }

    @GetMapping
    @ApiOperation("查询所有课程评论信息")
    public ResultVo list(){
        List<Comment> list = commentService.list(null);
        return ResultVo.ok().data("items",list);
    }

    /**
     * 查询指定的课程评论信息
     * @param id
     * @return
     */
    @ApiOperation("查询指定的课程评论信息")
    @GetMapping("{id}")
    public  ResultVo queryById(@ApiParam(name = "id",value = "指定查询的课程评论ID",required = true) @PathVariable String id){
        Comment comment = commentService.getById(id);
        return ResultVo.ok().data("item",comment);
    }

    /**
     * 删除指定的课程评论信息
     * @param id
     * @return
     */
    @ApiOperation("删除课程评论信息")
    @DeleteMapping("{id}")
    public ResultVo delete(@ApiParam(name = "id",value = "删除的课程评论ID",required = true) @PathVariable String id){
        boolean b = commentService.removeById(id);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 修改课程评论信息
     * @param comment
     * @return
     */
    @ApiOperation("修改课程评论信息")
    @PutMapping
    public ResultVo update(@RequestBody Comment comment){
        comment.setGmtModified(new Date());
        boolean b = commentService.updateById(comment);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 添加课程评论信息
     * @param comment
     * @return
     */
    @ApiOperation("添加课程评论信息")
    @PostMapping
    public ResultVo save(@RequestBody Comment comment){
        comment.setGmtModified(new Date());
        comment.setGmtCreate(new Date());
        boolean b = commentService.save(comment);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

}

